﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Benefit.Models.View;

namespace Benefit.Service.View
{
    /// <summary>
    /// 交易员月预期
    /// </summary>
    public class ServiceOperatorMonthExpected : Benefit.Interface.View.IOperatorMonthExpected
    {
        Benefit.DB.DBManager db = null;
        public ServiceOperatorMonthExpected(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        public void InitOperatorMonthExpected(int tradeHistoryId)
        {
            List<Models.Sys.Operator> operators = db.Operators.ToList();
            foreach (Models.Sys.Operator op in operators)
            {
                SetOperatorMonthExpected(op.Id, tradeHistoryId);
            }
            db.SaveChanges();
        }

        public void Delete(int year, int month)
        {
            var query = db.OperatorMonthExpected.Where(a => a.Month == month).Where(a => a.Year == year).ToList();
            foreach (OperatorMonthExpected op in query)
            {
                db.OperatorMonthExpected.Remove(op);
            }
            db.SaveChanges();
        }

        public OperatorMonthExpected GetOperatorMonthExpected(int operatorid, int year, int month)
        {
            var query = db.OperatorMonthExpected.Where(a => a.OperatorId == operatorid).Where(a => a.Month == month).Where(a => a.Year == year).OrderByDescending(a => a.Id).ToList();
            if (query.Count() > 0)
            {
                return query.First();
            }
            else
            {
                return null;
            }

        }

        /// <summary>
        /// 获取操作员周预期(这个方法应该每天去算，现在是从账户周预期里面查到，以后需要优化)
        /// </summary>
        /// <param name="operatorid"></param>
        /// <param name="week"></param>
        /// <param name="month"></param>
        /// <param name="year"></param>
        /// <returns></returns>
        public void SetOperatorMonthExpected(int operatorid, int tradeHistoryId)
        {
            OperatorMonthExpected change = new OperatorMonthExpected();
            Interface.Sys.ITradeHistory ith = new Sys.ServiceTradeHistory(db);
            Interface.Sys.IDayOperatorAccount idoa = new Sys.ServiceDayOperatorAccount(db);
            Models.Sys.TradeHistory th = db.TradeHistorys.Find(tradeHistoryId);
            change.Year = th.YPdate;
            change.Month = th.MPdate;
            change.OperatorId = operatorid;
            List<Models.Sys.Account> accounts = idoa.GetDayOperatorAccounts(ith.GetDateTimeFromTradeHistoryId(tradeHistoryId), operatorid);
            if (accounts.Count > 0)
            {
                foreach (Models.Sys.Account account in accounts)
                {
                    var query = from t in db.AccountMonthExpected where t.AccountId == account.Id && t.Month == th.MPdate && t.Year == th.YPdate select t;
                    if (query.Count() > 0)
                    {
                        change.BillCount += query.First().BillCount;
                        change.DayCount += query.First().DayCount;
                        change.Free += query.First().Free;
                        change.LoseCount += query.First().LoseCount;
                        change.OperatorId = operatorid;
                        change.Profit += query.First().Profit;
                        change.SumLost += query.First().SumLost;
                        change.SumWin += query.First().SumWin;
                        change.UsedMargin += query.First().UsedMargin;
                        change.WinCount += query.First().WinCount;
                    }
                }
            }
            db.OperatorMonthExpected.Add(change);
        }
    }
}
